package com.example.teamtribe.database;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class Posting extends DataSupport implements Parcelable, Comparable<Posting> {

    private int id;
    private int teamId;
    private int realId;
    private String title;
    private String content;
    private String createTime;
    private String editTime;
    private int targetId;
    private int comCnt;
    private List<String> images = new ArrayList<>();
    private int userId;

    public int getTeamId() {
        return teamId;
    }

    public void setTeamId(int teamId) {
        this.teamId = teamId;
    }

    public Posting() {
    }

    @Override
    public int compareTo(@NonNull Posting o) {
        return o.realId - realId > 0 ? 1 : -1;
    }

    public synchronized boolean saveOrUpdate() {
        return saveOrUpdate("realId = ?", realId + "");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Posting) {
            Posting posting = (Posting) obj;
            return this.realId == posting.realId;
        }
        return false;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getRealId() {
        return realId;
    }

    public void setRealId(int realId) {
        this.realId = realId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getEditTime() {
        return editTime;
    }

    public void setEditTime(String editTime) {
        this.editTime = editTime;
    }

    public int getTargetId() {
        return targetId;
    }

    public void setTargetId(int targetId) {
        this.targetId = targetId;
    }

    public int getComCnt() {
        return comCnt;
    }

    public void setComCnt(int comCnt) {
        this.comCnt = comCnt;
    }

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeInt(this.realId);
        dest.writeString(this.title);
        dest.writeString(this.content);
        dest.writeString(this.createTime);
        dest.writeString(this.editTime);
        dest.writeInt(this.targetId);
        dest.writeInt(this.comCnt);
        dest.writeStringList(this.images);
        dest.writeInt(this.userId);
    }

    protected Posting(Parcel in) {
        this.id = in.readInt();
        this.realId = in.readInt();
        this.title = in.readString();
        this.content = in.readString();
        this.createTime = in.readString();
        this.editTime = in.readString();
        this.targetId = in.readInt();
        this.comCnt = in.readInt();
        this.images = in.createStringArrayList();
        this.userId = in.readInt();
    }

    public static final Creator<Posting> CREATOR = new Creator<Posting>() {
        @Override
        public Posting createFromParcel(Parcel source) {
            return new Posting(source);
        }

        @Override
        public Posting[] newArray(int size) {
            return new Posting[size];
        }
    };
}
